﻿using System;
using System.Collections.Generic;
using System.Text;
using Lottery.App.Core;
using Lottery.App.Model;
using NHibernate.Criterion;

namespace Lottery.App.DAL
{
    public abstract class ResultDao<T> : AbstractDao<T> where T : ResultInfo
    {
        /// <summary>
        /// 查询一注方案在某买法下的中奖情况。用于报表导出。由各买法数据访问类重写。
        /// </summary>
        /// <param name="value">投注方案(升序排列)</param>
        internal virtual IList<RecordInfo> List(string value)
        {
            string hql = "from RecordInfo where ASC_Result like ?";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(value.Replace(" ", "%")))
                .List<RecordInfo>();
        }

        public IList<T> Search(string key)
        {
            key = key.Replace(" ", "%");

            return Session.CreateCriteria(typeof(T))
                .Add(Expression.Like("Value", key, NHibernate.Criterion.MatchMode.Anywhere))
                .AddOrder(NHibernate.Criterion.Order.Asc("LastID"))
                .List<T>();
        }

        public virtual IList<int> GetIds(string value)
        {
            const string hql = "select r.id from RecordInfo r where ASC_Result like ?";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(value.Replace(" ", "%")))
                .List<int>();
        }

        public IList<int> GetIds(string value, int start)
        {
            string hql = "select r.id from RecordInfo r where r.id > ? and ASC_Result like ?";

            return Session.CreateQuery(hql)
                .SetInt32(0, start)
                .SetString(1, MatchMode.Anywhere(value.Replace(" ", "%")))
                .List<int>();
        }

        public void Analyze()
        {
            IList<T> list = this.GetAllObjects();

            foreach (T item in list)
            {
                IList<int> ids = this.GetIds(item.Value);

                if (ids == null || ids.Count == 0)
                    continue;

                AnalyzeResult a = Tools.Analyze(ids);

                item.TotalCount = a.TotalCount;
                item.LastID = a.LastID;
                item.MaxOmit = a.MaxOmit;
                item.MaxOmitQH = Session.Get<RecordInfo>(a.MaxOmitID).IssueNum;
            }

            this.BeginTransaction();
            foreach (T item in list)
            {
                this.Update(item);
            }
            this.CommitTransaction();
        }

        protected virtual IList<T> GetItems(RecordInfo record)
        {
            return null;
        }

        public void UpdateRecord(RecordInfo record)
        {
            IList<T> list = this.GetItems(record);

            foreach (T item in list)
            {
                if (item.CurrentOmit > item.MaxOmit)
                {
                    item.MaxOmit = item.CurrentOmit - 1;
                    item.MaxOmitQH = record.IssueNum;
                }

                item.TotalCount++;
                item.LastID = record.ID;
            }

            this.BeginTransaction();
            foreach (T item in list)
            {
                this.Update(item);
            }
            this.CommitTransaction();
        }

    }
}
